#include "prpdinfotab.h"
#include "prpdfeature.h"
#include <QHeaderView>
#include <QHBoxLayout>
#include <QString>

PRPDInfoTab::PRPDInfoTab(QWidget *parent)
    : MapInfoTab(parent)
    , m_prpdFeature(std::make_shared<PRPDFeature>())
    , m_pChartBox(new QGroupBox("图谱数据"))
    , m_pChartData(new QTextEdit)
    , m_pMainLayout(new QHBoxLayout(this))
    , m_pLayout(new QHBoxLayout(this))
{
    // 创建布局
    m_pLayout->addWidget(m_pChartData);
    m_pChartBox->setLayout(m_pLayout);

    m_pMainLayout->addWidget(m_prpdFeature->initUI(this));
    m_pMainLayout->addWidget(m_pChartBox);

    setLayout(m_pMainLayout);

    // 添加PRPD特性组件
    addFeature(std::dynamic_pointer_cast<IMapInfoFeature>(m_prpdFeature));
}

PRPDInfoTab::~PRPDInfoTab()
{
    delete m_pChartBox;
    delete m_pChartData;
    delete m_pMainLayout;
    delete m_pLayout;
}

void PRPDInfoTab::setDataTypeCode(uint8_t code)
{
    // 数据会通过特性组件显示
    if (m_prpdFeature) {
        m_prpdFeature->setDataTypeCode(code);
    }
}

void PRPDInfoTab::setDataLength(int32_t length)
{
    // 数据会通过特性组件显示
    if (m_prpdFeature) {
        m_prpdFeature->setDataLength(length);
    }
}

void PRPDInfoTab::setCreateTime(const QString& time)
{
    // 数据会通过特性组件显示
    if (m_prpdFeature) {
        m_prpdFeature->setCreateTime(time);
    }
}

void PRPDInfoTab::setChartProperty(const DataSpecificationGW::SpectrumCharacter& property)
{
    // 数据会通过特性组件显示
    if (m_prpdFeature) {
        m_prpdFeature->setChartProperty(property);
    }
}

void PRPDInfoTab::setDeviceName(const QString& name)
{
    // 数据会通过特性组件显示
    if (m_prpdFeature) {
        m_prpdFeature->setDeviceName(name);
    }
}

void PRPDInfoTab::setDeviceCode(const QString& code)
{
    // 数据会通过特性组件显示
    if (m_prpdFeature) {
        m_prpdFeature->setDeviceCode(code);
    }
}

void PRPDInfoTab::setMeasurementName(const QString& name)
{
    // 数据会通过特性组件显示
    if (m_prpdFeature) {
        m_prpdFeature->setMeasurementName(name);
    }
}

void PRPDInfoTab::setMeasurementCode(const QString& code)
{
    // 数据会通过特性组件显示
    if (m_prpdFeature) {
        m_prpdFeature->setMeasurementCode(code);
    }
}

void PRPDInfoTab::setChannelFlag(int64_t flag)
{
    // 数据会通过特性组件显示
    if (m_prpdFeature) {
        m_prpdFeature->setChannelFlag(flag);
    }
}

void PRPDInfoTab::setDataType(uint8_t type)
{
    // 数据会通过特性组件显示
    if (m_prpdFeature) {
        m_prpdFeature->setDataType(type);
    }
}

void PRPDInfoTab::setAmplitudeUnit(uint8_t unit)
{
    if (m_prpdFeature) {
        m_prpdFeature->setAmplitudeUnit(unit);
    }
}

void PRPDInfoTab::setAmplitudeLowerLimit(float lowerLimit)
{
    if (m_prpdFeature) {
        m_prpdFeature->setAmplitudeLowerLimit(lowerLimit);
    }
}

void PRPDInfoTab::setAmplitudeUpperLimit(float upperLimit)
{
    if (m_prpdFeature) {
        m_prpdFeature->setAmplitudeUpperLimit(upperLimit);
    }
}

void PRPDInfoTab::setFrequencyBand(uint8_t band)
{
    // PRPD不需要处理频带
}

void PRPDInfoTab::setLowerFrequency(float lowerFreq)
{
    // PRPD不需要处理频率
}

void PRPDInfoTab::setUpperFrequency(float upperFreq)
{
    // PRPD不需要处理频率
}

void PRPDInfoTab::setPhaseWindowCount(int32_t count)
{
    if (m_prpdFeature) {
        m_prpdFeature->setPhaseWindowCount(count);
    }
}

void PRPDInfoTab::setQuantizedAmplitude(int n)
{
    if (m_prpdFeature) {
        m_prpdFeature->setQuantizedAmplitude(n);
    }
}

void PRPDInfoTab::setFrequencyPeriod(int32_t num)
{
    if (m_prpdFeature) {
        m_prpdFeature->setFrequencyPeriod(num);
    }
}

void PRPDInfoTab::setDischargeTypeProbability(const QString& probability)
{
    // PRPD不需要处理放电类型概率
}

void PRPDInfoTab::setBackgroundFileName(const QString& fileName)
{
    // PRPD不需要处理背景文件名称
}

void PRPDInfoTab::setReservedData(const QByteArray& byte)
{
    // 数据会通过特性组件显示
    if (m_prpdFeature) {
        m_prpdFeature->setReservedData(byte);
    }
}

void PRPDInfoTab::setPRPDData(const QByteArray& byte)
{
    if (m_prpdFeature) {
        m_prpdFeature->setPRPDData(byte);
    }
    
    // 在图表数据框显示数据的十六进制表示
    m_pChartData->setText(byte.toHex());
}

QTableWidgetItem *PRPDInfoTab::createTableWidgetItem(const QString &text)
{
    QTableWidgetItem *item = new QTableWidgetItem();
    QFont font;
    font.setBold(true);
    item->setFont(font);
    item->setTextAlignment(Qt::AlignCenter);
    item->setText(text);
    return item;
}

